package com.yzh.openai.domain.openai.service.rule.factory;

import com.yzh.openai.domain.openai.annotation.LogicStrategy;
import com.yzh.openai.domain.openai.service.rule.ILogicFilter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 规则工厂
 */
@Service
public class DefaultLogicFactory {
    public Map<String, ILogicFilter> logicFilterMap = new ConcurrentHashMap<>();
    /**
     * 这里的接收一个列表是ILogicFilter类型的，spring会自动寻找ILogicFilter类型的Bean注入！！！！！
     */
    public DefaultLogicFactory(List<ILogicFilter> logicFilters) {
        logicFilters.forEach(item -> {
            LogicStrategy strategy = AnnotationUtils.findAnnotation(item.getClass(), LogicStrategy.class);
            if (null != strategy) {
                logicFilterMap.put(strategy.logicMode().getCode(), item);
            }
        });
    }

    public Map<String, ILogicFilter> getLogicFilter() {
        return logicFilterMap;
    }

    /**
     * 规则逻辑枚举
     */
    public enum LogicModel {
        NULL("NULL", "放行不用过滤"),
        SENSITIVE_WORD("SENSITIVE_WORD", "敏感词过滤"),
        USER_QUOTA("USER_QUOTA", "用户额度过滤"),
        MODEL_TYPE("MODEL_TYPE", "模型可用范围过滤"),
        ACCOUNT_STATUS("ACCOUNT_STATUS", "账户状态过滤"),
        ;
        
        private final String code;
        private final String info;

        LogicModel(String coe, String info) {
            this.code = coe;
            this.info = info;
        }

        public String getCode() {
            return code;
        }

        public String getInfo() {
            return info;
        }
    }
}
